import java.io.*;
import java.util.List;

/**
 * Created by xwj on 2017/9/1.
 */
public class MakeFile {

    // (0,0)在最左上角
    private static String initialBlock = "1-0";
    private static double xPart = 0.25, yPart = 0.5;//截取后的图像占比
    private static int width = 1920, height = 1080;
    private static int partWidth = (int) (xPart * width), partHeight = (int) (yPart * height);
    private static int Ysize = height * width, UVsize = Ysize / 4;

    //要先选出segment，在选出segment对应的
    public static void makePredictionFile(String prefix,List<Point> pointList,List<Point> segmentPoints, int onlyY, String destFile) {
        Reader reader = null;
        Writer writer = null;
        char[] tmpY = new char[Ysize];
        char[] tmpUV = new char[UVsize];
        int[] origcenter = new int[2];
        int charread = 0;
        char[] newY = new char[partHeight * partWidth];
        try {
            writer = new OutputStreamWriter(new FileOutputStream(destFile));
            int frame=0;
            for (int i = 0; i < segmentPoints.size(); i++) {
                String predictFile = null;
                if (i == 0) {
                    predictFile = prefix + initialBlock;
                } else {
                    predictFile = prefix + Prediction.predictPointByPreviousInfo(segmentPoints.get(i - 1), segmentPoints.get(i), 0.8, 0.8);
                }
                reader = new InputStreamReader(new FileInputStream(predictFile));
                frame=writeFile(reader,writer,frame,pointList);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static void makeOrigiFile(String fileName, List<Point> pointList, int onlyY, String destFile) {
        Reader reader = null;
        Writer writer = null;
        char[] tmpY = new char[Ysize];
        char[] tmpUV = new char[UVsize];
        int[] origcenter = new int[2];
        int charread = 0;
        char[] newY = new char[partHeight * partWidth];
        //char[] newUV=new char[partHeight*partWidth>>2];

        try {
            writer = new OutputStreamWriter(new FileOutputStream(destFile));
            reader = new InputStreamReader(new FileInputStream(fileName));
            int frame = 0;
            frame=writeFile(reader,writer,frame,pointList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) reader.close();
                if (writer != null) writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static int[] getCorrespondingPoint(int x, int y, int[] origcenter) {
        int[] origpoint = new int[2];

        //这里取的是一个矩形范围，你可以有更好的映射方法
        origpoint[0] = Util.clip(x - (partWidth >> 1) + origcenter[0], 0, width);
        origpoint[1] = Util.clip(y - (partHeight >> 1) + origcenter[1], 0, height);

        return origpoint;
    }


    //return frame数量
    public static int writeFile(Reader reader ,Writer writer,int frame,List<Point> pointList){
        char[] tmpY = new char[Ysize];
        char[] tmpUV = new char[UVsize];
        int[] origcenter = new int[2];
        int charread = 0;
        char[] newY = new char[partHeight * partWidth];
        //char[] newUV=new char[partHeight*partWidth>>2];
        try {
            // 读入多个字符到字符数组中，charread为一次读取字符数
            while ((charread = reader.read(tmpY)) != -1) {
                if (frame >= pointList.size()) {
                    System.out.println("观看记录已经结束！");
                    break;
                }
                // 同样屏蔽掉\r不显示
                if (charread == tmpY.length) {
                    Point center = pointList.get(frame);
                    origcenter[0] = (int) center.getX() * width;
                    origcenter[1] = (int) center.getY() * height;
                    for (int i = 0; i < partHeight; i++) {
                        for (int j = 0; j < partWidth; j++) {
                            int[] origpoint = getCorrespondingPoint(j, i, origcenter);
                            newY[i * partHeight + j] = tmpY[origpoint[1] * width + origpoint[0]];
                        }
                    }
                    writer.write(newY);

                } else {
                    System.out.println("tmpY 未读到足够的char");
                }

                //暂时不管UV
                charread = reader.read(tmpUV);
                charread = reader.read(tmpUV);
                frame++;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return frame;
    }

}
